<?php
	function habilitation()
	{
		$hab = array(
			1,
			2
		);
		
		if (in_array(get_s('bo_user_role'), $hab))
			return true;
		return false;
	}
	
	function edit($table, $id)
	{
		if (habilitation())
		{
			$message = '';
			$champs = champs();
			foreach ($champs as $champ)
			{
				$val = $_POST[$champ["id"]];
				if ($champ["obligatoire"] && !strlen($val))
					$message .= 'Le champ <strong>' . trad($champ["Affichage"], APPLNG) . '</strong> est vide mais obligatoire.<br />';
			}
			
			if (strlen($message))
			{
				$retour["statut"] = false;
				$retour["message"] = $message;
			}
			else
			{
				$gp_fonc = GP_fonctions::getInstance();
				$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
				GP_ActiveRecord::setDefaultDBConnection($gp_db);
				
				$now = date("Y-m-d H:i:s");
				$add = new GP_ActiveRecord($table, $id);
				$add -> datemodif = $now;
				foreach ($champs as $champ)
				{
					$val = $_POST[$champ["id"]];
					if ($champ["type"] == 'password')
						$val = md5($val);
					if ($champ["type"] == 'file' || $champ["type"] == 'image')
						$val = $_POST[$champ["id"] . "_file"];
					$add -> $champ["id"] = $val;
				}
				
				$add -> soumettre();
				
				$retour["statut"] = true;
			}
			return $retour;
		}
		return false;
	}
	
	function champs()
	{
		$retour = array();
		
		$retour['id_line']['id'] = 'id_line';
		$retour['id_line']['Affichage'] = 'Ligne produit';
		$retour['id_line']['type'] = 'select';
		$retour['id_line']['typeselect'] = 'sql';
		$retour['id_line']['data'] = 'SELECT id, name FROM _fo_lines';
		$retour['id_line']['premierchoix'] = trad('Choisir', APPLNG) . '%%';
		$retour['id_line']['defaut'] = '';
		$retour['id_line']['js'] = 'return false;';
		$retour['id_line']['obligatoire'] = true;
		
		$retour['ordre']['id'] = 'ordre';
		$retour['ordre']['Affichage'] = 'Ordre';
		$retour['ordre']['max'] = 3;
		$retour['ordre']['type'] = 'input';
		$retour['ordre']['obligatoire'] = true;
		
		$retour['num_image']['id'] = 'num_image';
		$retour['num_image']['Affichage'] = 'Numéro de l\'image';
		$retour['num_image']['max'] = 10;
		$retour['num_image']['type'] = 'input';
		$retour['num_image']['obligatoire'] = true;
		
		$retour['link_eshop']['id'] = 'link_eshop';
		$retour['link_eshop']['Affichage'] = 'Lien eshop';
		$retour['link_eshop']['max'] = 255;
		$retour['link_eshop']['type'] = 'input';
		$retour['link_eshop']['obligatoire'] = true;
		
		$retour['name']['id'] = 'name';
		$retour['name']['Affichage'] = 'Nom';
		$retour['name']['max'] = 255;
		$retour['name']['type'] = 'input';
		$retour['name']['obligatoire'] = true;
		
		$retour['type']['id'] = 'type';
		$retour['type']['Affichage'] = 'Type';
		$retour['type']['max'] = 255;
		$retour['type']['type'] = 'input';
		$retour['type']['format'] = 'jpg';
		$retour['type']['obligatoire'] = true;
		
		$retour['contenance']['id'] = 'contenance';
		$retour['contenance']['Affichage'] = 'Contenance';
		$retour['contenance']['max'] = 255;
		$retour['contenance']['type'] = 'input';
		$retour['contenance']['obligatoire'] = true;
		
		$retour['description']['id'] = 'description';
		$retour['description']['Affichage'] = 'Description';
		$retour['description']['max'] = 5000;
		$retour['description']['type'] = 'textarea';
		$retour['description']['obligatoire'] = true;
		
		$retour['fragrances']['id'] = 'fragrances';
		$retour['fragrances']['Affichage'] = 'Fragrances';
		$retour['fragrances']['max'] = 5000;
		$retour['fragrances']['type'] = 'textarea';
		$retour['fragrances']['obligatoire'] = true;
		
		$retour['meta_description']['id'] = 'meta_description';
		$retour['meta_description']['Affichage'] = 'Meta description';
		$retour['meta_description']['max'] = 5000;
		$retour['meta_description']['type'] = 'textarea';
		$retour['meta_description']['obligatoire'] = true;
		
		$retour['meta_keywords']['id'] = 'meta_keywords';
		$retour['meta_keywords']['Affichage'] = 'Meta keywords';
		$retour['meta_keywords']['max'] = 5000;
		$retour['meta_keywords']['type'] = 'textarea';
		$retour['meta_keywords']['obligatoire'] = true;
		
		$retour['page_title']['id'] = 'page_title';
		$retour['page_title']['Affichage'] = 'Titre page';
		$retour['page_title']['max'] = 255;
		$retour['page_title']['type'] = 'input';
		$retour['page_title']['obligatoire'] = true;
		
		$retour['alt_index']['id'] = 'alt_index';
		$retour['alt_index']['Affichage'] = 'Alt index';
		$retour['alt_index']['max'] = 255;
		$retour['alt_index']['type'] = 'input';
		$retour['alt_index']['obligatoire'] = true;
		
		$retour['alt_page_product']['id'] = 'alt_page_product';
		$retour['alt_page_product']['Affichage'] = 'Alt page produit';
		$retour['alt_page_product']['max'] = 255;
		$retour['alt_page_product']['type'] = 'input';
		$retour['alt_page_product']['obligatoire'] = true;

		return $retour;
	}
	
	function datamaj($table, $id)
	{
		$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
		$sql = "SELECT * FROM $table WHERE id = $id";
		$tab = $gp_db -> fetchArray_one($sql);
		return $tab;
	}
	
	$message_ok = false;
	if ($this -> action_form == 1)
	{
		$form = edit($this -> table, $this -> id);
		$this -> setvar('datamaj', $_POST);
		if (!$form["statut"])
		{
			$message_erreur = $form["message"];
			$this -> setvar('message_erreur', $message_erreur);
		}
		else
			$message_ok = true;
	}
	else
		$this -> setvar('datamaj', datamaj($this -> table, $this -> id));
	
	$this -> setvar('message_ok', $message_ok);
	$this -> setvar('titre', 'Editer une ligne de produits');
	$this -> setvar('retour_url', rw('contenus'));
	$this -> setvar('habilitation', habilitation());
	$this -> setvar('champs', champs());
?>